{This is Pascal source code which will compile into}
{an xCOD (eXternal CODe) resource for NumberCrunch II.}
{}
{All variables passed MUST be declared var and }
{MUST be extended or array of extended or external procedures.}
{}
{ After building this code resource you must install it into NumberCrunch with either ResEdit or the}
{ Load xCOD command from inside NCII. }
{}
{ In either case, an sCOD string or resource is also needed to tell NCII what the arguments to this routine are.}
{ For this routine, the appropriate sCOD is:}
{}
{ xCOD xSAMPLE_XCODE(J:num; Bj:Array; prog Foo(K:num; Ak:array); Cj:ComplexArray), sets all of Cj[1…J] to 1 + 0i, Bj[1…j] to 1…j, and passes a K=3 element array [3,2,1] to Foo.}
{}
{ The procedure arguments are}
{ J : extended number}
{ Bj : array of extended}
{ Foo : the name of another xCOD or an NCII user program }
{ Cj : an array of complex numbers, i.e. a 2N array of extended.}
{}
{}
{ •••••••••••••••••}
{ • SAMPLE_XCOD •}
{ •••••••••••••••••}
{}
{ This xCOD just shows how to pass complex quantities and external routines in Pascal.}
{ To incorporate this into a Project, }
{ • include the files}
{ DRVRRuntime.lib}
{ Interface.lib}
{ SANELib.lib}
{ SANE.p}
{ this file, SAMPLE_XCOD.p}
{ • In Set Project Type under the Project menu, set}
{ Code Resource, }
{ File: Type= 'rsrc'; Creator='RSED'}
{ Resource: Name = 'SAMPLE_XCOD', Type='xCOD', }
{ ID=42 (or any unique ID.)}
{ }
{ }
unit SAMPLE_XCOD;
interface
uses
SANE;
type
{ Array of extended numbers. The 1..1 range just ensures that no range}
{ checking is done. If you write outside the range that is reserved by the}
{ calling routine, the system will probably crash.}
{}
ExtendedArrayType = array[1..1] of extended;
{ Pascal does not support complex numbers, so usually it's easier to}
{ treat complex arrays as ExtendedArrayType which are packed with}
{ real and complex parts alternating, i.e. a 2N array (r1,c1,r2,c2,r3,c3...,rN,cN). }
{ However, one could use these types for pascal complex numbers a.r, a.c and }
{ complex arrays b[j].r and b[j].c. }
{}
ComplexNumberType = record
r, c: extended;
end;
ComplexArrayType = array[1..1] of ComplexNumberType;
{ The procedure arguments are}
{ eJ : extended number}
{ Bj : array of extended}
{ Foo : the name of another xCOD or an NCII user program }
{ Cj : an array of complex numbers, i.e. a 2N array of extended.}
procedure Main (var eJ: extended; {}
var Bj: ExtendedArrayType; {}
FooPtr: Ptr; {}
var Cj: ExtendedArrayType);
implementation
{ To pass an array of 3 extended numbers to the external procedure Foo, }
{ we must have an actual variable of that length. }
{ We cannot use ExtendedArrayType, because it is only declared to be }
{ of size [1..1], which avoids any range checking on passed variables.}
const
OtherSize = 3; {The size of the array to be passed to Foo}
type
ThreeExtendedType = array[1..OtherSize] of extended;
{This procedure must have arguments that match the declaration of }
{the program "foo" in the sCOD line above, with the "address" argument added.}
{The inline code here just strips the "address" from the argument stack and }
{jumps to Address, which is the address of the real foo routine passed as a Pointer.}
{}
{ Note that since the array that will be sent is of ThreeExtendedType rather}
{ than the vanilla ExtendedArrayType, that is how "a" is declared here.}
{}
procedure fooInterface (var n: extended; var a: ThreeExtendedType; {}
address: Ptr);
inline
$205F, $4E90;
procedure Main;
var
J, i: integer;
anArrayForFoo: ThreeExtendedType;
SizeForFoo: extended;
begin
J := Num2Integer(eJ); { This is a SANE routine that converts extended to integer }
{ Set the elements of Bj to 1,2,3,… }
for i := 1 to J do
Bj[i] := 1.0 * i;
{ Set all the real parts of Cj to 1, and the imaginary parts to 0.}
for i := 1 to J do
begin
Cj[2 * i - 1] := 1.0; {real parts}
Cj[2 * i] := 0.0; {complex parts}
end;
AnArrayForFoo[1] := 3.0; { Set up the array to pass to Foo.}
AnArrayForFoo[2] := 2.0;
AnArrayForFoo[3] := 1.0;
SizeForFoo := OtherSize; { This size must be passed as an extended, so we put it into a variable of that type.}